{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "28d05cee",
   "metadata": {},
   "source": [
    "### 自动划分区间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b2a711e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def auto_range(df,column):\n",
    "    # 自动化分区间函数\n",
    "    aaa = df[df[column]>0][column]\n",
    "    bins=aaa.quantile([i/100 for i in range(10,101,5)]).values\n",
    "    a,b = pd.cut(df[column],bins=bins,retbins=True)\n",
    "    bbb = pd.DataFrame(a.value_counts(normalize=True)).reset_index().sort_values(by=\"index\")\n",
    "    bbb[\"cumsum\"]=bbb[\"auto_num_6m\"].cumsum()\n",
    "    return bbb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0405e2e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27021b38",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fa8b4393",
   "metadata": {},
   "source": [
    "### 获取指定日期的周一和周日的日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "04857b05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('2022-02-14', '2022-02-20')\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime,timedelta\n",
    "\n",
    "\n",
    "def get_monday_to_sunday(today, weekly=0):\n",
    "    \"\"\"\n",
    "    获取指定日期的周一和周日的日期\n",
    "    :param today: 当前日期\n",
    "    :param weekly: 获取指定日期的上几周或者下几周，0当前周，-1上一周，1下一周\n",
    "    :return: 返回指定日期的周一和周日日期\n",
    "    \"\"\"\n",
    "    last = weekly * 7\n",
    "    today = datetime.strptime(str(today), \"%Y-%m-%d\")\n",
    "    monday = datetime.strftime(today - timedelta(today.weekday() - last), \"%Y-%m-%d\")\n",
    "    monday_ = datetime.strptime(monday, \"%Y-%m-%d\")\n",
    "    sunday = datetime.strftime(monday_ + timedelta(monday_.weekday() + 6), \"%Y-%m-%d\")\n",
    "    return monday, sunday\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    result = get_monday_to_sunday(\"2022-02-16\")\n",
    "    print(result)  # ('2022-02-14', '2022-02-20')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05a9f778",
   "metadata": {},
   "source": [
    "### pandas同时赋值两列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "444c7c43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>23</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>26</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>37</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>D</td>\n",
       "      <td>46</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>E</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>F</td>\n",
       "      <td>12</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>G</td>\n",
       "      <td>53</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>H</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>I</td>\n",
       "      <td>66</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>J</td>\n",
       "      <td>32</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  age  score\n",
       "0    A   23     13\n",
       "1    B   26     23\n",
       "2    C   37     22\n",
       "3    D   46     76\n",
       "4    E   85     56\n",
       "5    F   12     89\n",
       "6    G   53     99\n",
       "7    H   80    100\n",
       "8    I   66     10\n",
       "9    J   32     54"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df = pd.DataFrame(data={\n",
    "    \"name\":[\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\"],\n",
    "    \"age\":[23,26,37,46,85,12,53,80,66,32],\n",
    "    \"score\":[13,23,22,76,56,89,99,100,10,54],\n",
    "})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "760a255b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "      <th>age1</th>\n",
       "      <th>age2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>23</td>\n",
       "      <td>13</td>\n",
       "      <td>24</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>26</td>\n",
       "      <td>23</td>\n",
       "      <td>27</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>37</td>\n",
       "      <td>22</td>\n",
       "      <td>38</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>D</td>\n",
       "      <td>46</td>\n",
       "      <td>76</td>\n",
       "      <td>47</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>E</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>86</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>F</td>\n",
       "      <td>12</td>\n",
       "      <td>89</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>G</td>\n",
       "      <td>53</td>\n",
       "      <td>99</td>\n",
       "      <td>54</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>H</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>81</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>I</td>\n",
       "      <td>66</td>\n",
       "      <td>10</td>\n",
       "      <td>67</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>J</td>\n",
       "      <td>32</td>\n",
       "      <td>54</td>\n",
       "      <td>33</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  age  score  age1  age2\n",
       "0    A   23     13    24    25\n",
       "1    B   26     23    27    28\n",
       "2    C   37     22    38    39\n",
       "3    D   46     76    47    48\n",
       "4    E   85     56    86    87\n",
       "5    F   12     89    13    14\n",
       "6    G   53     99    54    55\n",
       "7    H   80    100    81    82\n",
       "8    I   66     10    67    68\n",
       "9    J   32     54    33    34"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fun(x):\n",
    "    return x+1,x+2\n",
    "\n",
    "df[[\"age1\",\"age2\"]] = df.apply(lambda x:fun(x[\"age\"]),axis=1,result_type=\"expand\")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c99303bc",
   "metadata": {},
   "source": [
    "### 列表推导式高级用法案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "59c4543a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 2, 2, 3, 3, 4, 4]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[n for e in [[i]*2 for i in range(1,5)] for n in e]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0794906a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
